package com.gexiaobao.pigeon.ui.fragment.mine.tools.query

import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.view.inputmethod.EditorInfo
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.eventViewModel
import com.gexiaobao.pigeon.app.ext.RxTextTool
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initFooter
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.model.bean.FindPigeonResponse
import com.gexiaobao.pigeon.app.network.ListDataUiState
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.databinding.FragmentSearchRingBinding
import com.gexiaobao.pigeon.ui.adapter.FindPigeonListAdapter
import com.gexiaobao.pigeon.viewmodel.MineToolsViwModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2023/8/3 15:17
 * @Describe    : 搜环号
 */
class FragmentSearchRing : BaseFragment<MineToolsViwModel, FragmentSearchRingBinding>() {

    private var ringNumber = ""
    private val mAdapter: FindPigeonListAdapter by lazy { FindPigeonListAdapter(arrayListOf()) }
    private lateinit var loadsir: LoadService<Any>

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        activity?.let { StatusBarUtil.setLightMode(it) }
        activity?.let { StatusBarUtil.setColor(it, Color.TRANSPARENT, 0) }
    }

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.includeSearch.ivClean.visibility = View.VISIBLE
        mDatabind.includeSearch.editQuery.hint = "请输入完整足环号搜索"
        ringNumber = arguments?.getString("ringNumber").toString().trim()
        mDatabind.includeSearch.editQuery.setText(ringNumber)

        initAdapter()

        /** 状态页配置 */
        loadsir = loadServiceInit(mDatabind.layoutList.swipeRefreshLayout) {
            /** 点击重试时触发的操作 */
            showLoadingAntiShake()
            mViewModel.searchPigeon(true, ringNumber)
        }

        /** 下拉刷新 */
        mDatabind.layoutList.swipeRefreshLayout.init {
            showLoadingAntiShake()
            mViewModel.searchPigeon(true, ringNumber)
        }
        mAdapter.addChildClickViewIds(R.id.itemFindPigeonDetail)
        mAdapter.setOnItemChildClickListener { _, _, position ->
            nav().navigateAction(R.id.action_to_fragment_race_detail, Bundle().apply {
                putString("ringId", mAdapter.data[position].ringId)
            })
        }

        mDatabind.includeSearch.editQuery.setOnEditorActionListener { _, i, _ ->
            when (i) {
                EditorInfo.IME_ACTION_SEARCH ->
                    searchPigeon()

                else -> {}
            }
            false
        }
    }

    private fun initAdapter() {
        mDatabind.layoutList.swipeRecyclerView.init(LinearLayoutManager(context), mAdapter).let {
            it.initFooter {
                //触发加载更多时请求数据
                showLoadingAntiShake()
                mViewModel.searchPigeon(true, ringNumber)
            }
        }
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        showLoading(getString(R.string.loading))
        mViewModel.searchPigeon(true, ringNumber)
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.findPigeonResult.observe(viewLifecycleOwner) {
            dismissLoadingAntiShake()
            dismissLoading()
            eventViewModel.toNoticeRingId.value = ringNumber
            loadListData(it, mAdapter, loadsir, mDatabind.layoutList.swipeRecyclerView, mDatabind.layoutList.swipeRefreshLayout)
//            showDataUI(it)
        }
    }

//    private fun showDataUI(it: ListDataUiState<FindPigeonResponse.ListBean>?) {
//        if (it != null) {
//            RxTextTool.getBuilder("")
//                .append("已为您找到同样或相似足环对应 ")
//                .append(it.total.toString()).setBold().setAppearance(50).setTextColor(Color.parseColor("#EB3E3E"))
//                .append(" 条记录")
//                .into(mDatabind.tvTotalData)
//        } else {
//            mDatabind.tvTotalData.visibility = View.GONE
//        }
//    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeSearch.ivBack, mDatabind.includeSearch.tvSearch, mDatabind.includeSearch.ivClean) {
            when (it) {
                mDatabind.includeSearch.ivBack -> {
                    nav().navigateUp()
                }

                mDatabind.includeSearch.ivClean -> {
                    mDatabind.includeSearch.editQuery.setText("")
                }

                mDatabind.includeSearch.tvSearch -> {
                    searchPigeon()
                }
            }
        }
    }

    private fun searchPigeon() {
        ringNumber = mDatabind.includeSearch.editQuery.text.toString().trim()
        showLoading(getString(R.string.loading))
        mViewModel.searchPigeon(true, ringNumber)
    }
}